//
//  LeanCloudRegisterViewController.m
//  WeChatQQStaticLibDemo
//
//  Created by mac on 17/2/9.
//  Copyright © 2017年 srulos. All rights reserved.
//

#import "LeanCloudRegisterViewController.h"
#import "WeChatQQStaticLibHeader.h"

@interface LeanCloudRegisterViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;

@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIButton *registerNowBtn;
@end

@implementation LeanCloudRegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    [self setUpViews];
}


-(void)setUpViews{
    self.title = @"Register";

    dispatch_async(dispatch_get_main_queue(), ^{
        self.registerNowBtn.layer.cornerRadius=5;
        
        UIImageView *userNameImage= [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_username.png"]];
        userNameImage.frame = CGRectMake(10, 5, 15, 15);
        userNameImage.contentMode= UIViewContentModeScaleAspectFit;
        UIView *userNameLeftView = [[UIView alloc]initWithFrame:CGRectMake(10, 0, 25, 30)];
        [userNameLeftView addSubview:userNameImage];
        self.userNameTextField.leftView = userNameLeftView;
        self.userNameTextField.leftViewMode = UITextFieldViewModeAlways;
        
        UIImageView *passwordImage=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"login_psd.png"]];
        passwordImage.frame=CGRectMake(10, 5, 15, 15);
        passwordImage.contentMode=UIViewContentModeScaleAspectFit;
        UIView *passwordLeftView=[[UIView alloc]initWithFrame:CGRectMake(10, 0, 25, 30)];
        [passwordLeftView addSubview:passwordImage];
        self.passwordTextField.leftView=passwordLeftView;
        self.passwordTextField.leftViewMode=UITextFieldViewModeAlways;
    });
}

- (IBAction)onBackToLoginCallBack:(id)sender {
    [self.navigationController popViewControllerAnimated:true];
}

- (IBAction)onRegisterCallBack:(id)sender {
    if (_userNameTextField.text.length >= 6 && _passwordTextField.text.length >= 6) {
        
        NSString * username = self.userNameTextField.text;
        NSString * password = self.passwordTextField.text;
        
        if (username && password) {
            SHOW_HUD(@"Is registering...");
            
            [[AsyncLeanCloudSimulationService stander]asyncRegisterLeanCloud:username password:password respBlock:^(LEANCLOUD_REGISTER_RESULT result) {
                HIDE_HUD;
                if (result == LRR_NOT_INIT) {
                    NSString *msg = [self getLocalizedStr:@"The network does not seem to work. Please try again later"];
                    NSString *hint = [self getLocalizedStr:@"Hint"];
                    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:hint message:msg delegate:nil cancelButtonTitle:[self getLocalizedStr:@"Confirm"] otherButtonTitles:nil, nil];
                    [alertview show];
                }
                else if (result == LRR_SUCCESS) {
                    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"Hint" message:@"registration success! please sign in！" delegate:nil cancelButtonTitle:@"Confirm" otherButtonTitles:nil];
                    [alertView show];
                    
                    [self onBackToLoginCallBack:nil];
                }
                else{
                    UIAlertView * alertView = [[UIAlertView alloc]initWithTitle:@"Hint" message:@"failure! Username already exists! Please try again!" delegate:nil cancelButtonTitle:@"Confirm" otherButtonTitles:nil];
                    [alertView show];
                }
            }];
        }
    } else {
        SHOW_HUD_MESSAGE(@"Please fill in the correct account or password! Account password must be less than 6!");
    }
}





-(NSString *)getLocalizedStr:(NSString *)str{
    return NSLocalizedStringFromTable(str, @"HHLeanCloud", nil);
}

@end
